package me.kuye.jkuye.plugin.activerecord;

import java.util.Collections;
import java.util.Map;

import me.kuye.jkuye.util.StringUtil;

public class Table {
	private String name;
	private String[] primaryKey;
	private Map<String, Class<?>> columnTypeMap;
	private Class<? extends Model<?>> modelClass;

	public Table(String tableName, String primaryKey, Class<? extends Model<?>> modelClass) {
		if (StringUtil.isEmpty(name)) {
			throw new IllegalArgumentException("the table name can not be null");
		}
		if (modelClass == null) {
			throw new IllegalArgumentException("the table class can not be null");
		}
		this.name = tableName.trim();
		this.modelClass = modelClass;
		setPrimaryKey(primaryKey);
	}

	public Table(String tableName, Class<? extends Model<?>> modelClass) {
		if (StringUtil.isEmpty(name)) {
			throw new IllegalArgumentException("the table name can not be null");
		}
		if (modelClass == null) {
			throw new IllegalArgumentException("the table class can not be null");
		}
		this.name = tableName.trim();
		this.modelClass = modelClass;
	}

	void setPrimaryKey(String primaryKey) {
		String[] primaryKeys = primaryKey.split(",");
		for (int i = 0; i < primaryKeys.length; i++)
			primaryKeys[i] = primaryKeys[i].trim();
		this.primaryKey = primaryKeys;
	}

	void setColumnTypeMap(Map<String, Class<?>> columnTypeMap) {
		if (columnTypeMap == null)
			throw new IllegalArgumentException("columnTypeMap can not be null");

		this.columnTypeMap = columnTypeMap;
	}

	public String getName() {
		return name;
	}

	void setColumnType(String columnLabel, Class<?> columnType) {
		columnTypeMap.put(columnLabel, columnType);
	}

	public Class<?> getColumnType(String columnLabel) {
		return columnTypeMap.get(columnLabel);
	}

	public boolean hasColumnLabel(String columnLabel) {
		return columnTypeMap.containsKey(columnLabel);
	}

	public String[] getPrimaryKey() {
		return primaryKey;
	}

	public Class<? extends Model<?>> getModelClass() {
		return modelClass;
	}

	public Map<String, Class<?>> getColumnTypeMap() {
		return Collections.unmodifiableMap(columnTypeMap);
	}

}
